<template>
  <div class="login">
    <div class="logoImage">
      <img src="@/assets/images/indexAndLogin/loginLogo.png" alt="">
    </div>
    <div class="form-container">
      <div class="welcomeText">
        <h2>欢迎登录</h2>
        <p></p>
      </div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            placeholder="请输入用户名"
          >
            <svg-icon slot="prefix" icon-class="user" color="#0088ff" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="请输入密码"
            show-password
            @keyup.enter.native="handleLogin"
          >
            <svg-icon slot="prefix" icon-class="password" color="#0088ff" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item style="width:100%;">
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="width:100%;margin-top: 20px"
            @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import Cookies from "js-cookie"
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
      },
      loading: false,
      // 注册开关
      register: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCookie()
  },
  methods: {
    getCookie() {
      const username = Cookies.get("username")
      const password = Cookies.get("password")
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 })
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 })
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 })
          } else {
            Cookies.remove("username")
            Cookies.remove("password")
            Cookies.remove('rememberMe')
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{})
          }).catch(() => {
            this.loading = false
            if (this.captchaEnabled) {
              this.getCode()
            }
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.login {
  height: 100%;
  background-image: url("../assets/images/indexAndLogin/loginBgi.png");
  background-size: 100% 100%;
  background-repeat: no-repeat;
  position: relative;
  .logoImage{
    width: 534px;
    height: 85px;
    position: absolute;
    left: 40px;
    top: 40px;
    img{
      width: 100%;
      height: 100%;
    }
  }
  .form-container{
    width: 344px;
    height: 453px;
    background-image: url('../assets/images/indexAndLogin/loginForm.png');
    background-size: 100% 100%;
    background-repeat: no-repeat;
    position: absolute;
    right: 200px;
    top: 50%;
    transform: translateY(-50%);
    padding: 40px 30px;
    box-sizing: border-box;
     .welcomeText{
       h2{
         margin-top: 50px;
         font-family: Source Han Sans, Source Han Sans;
         font-weight: 500;
         font-size: 30px;
         color: #384A5C;
       }
       p{
         font-family: Source Han Sans, Source Han Sans;
         font-weight: 400;
         font-size: 22px;
         color: #384A5C;
         margin: 20px 0 30px 0;
       }

     }
  }
}

</style>
